package com.wl.api.controller.system;

import java.util.Date;
import java.util.function.Predicate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.wl.api.common.utils.ErrorCodeEnum;
import com.wl.api.common.utils.JsonObject;
import com.wl.api.common.utils.ObjectUtil;
import com.wl.api.controller.base.CommonController;
import com.wl.api.dto.base.CommonUserDTO;
import com.wl.api.dto.business.CustomerFundDetailDTO;
import com.wl.api.service.business.ICustomerFundDetailService;
import com.wl.api.service.business.ICustomerFundService;
/**
 * 后台资金管理
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/admin/customer/fund")
public class AdminFundController extends CommonController {
	
	@Autowired
	ICustomerFundDetailService fundDetailService;
	@Autowired
	ICustomerFundService fundService;
	
	@RequestMapping(value = "/audit", method = RequestMethod.POST)
	public JsonObject checkFund(@RequestBody CustomerFundDetailDTO param) throws Exception  {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "", code = "";
		if(validate().test(param)) {
			title = ErrorCodeEnum.param_error.getError_code();
			code = ErrorCodeEnum.param_error.getMessage();
			pass = false;
		}
		if (pass) {
			initUserData(param);
			fundService.updateCustomerFund(param);
			json.setData(param.getFundDetailId());
		}
		setJsonMessage(json, title, code, pass);
		return json;
	}
	
	@RequestMapping(value = "/audit", method = RequestMethod.GET)
	public JsonObject fundItem(CustomerFundDetailDTO param) throws Exception  {
		JsonObject json = JsonObject.getInstance();
		json.setPage(fundDetailService.getItemList(param));
		setJsonMessage(json);
		return json;
	}
	
	private void initUserData(CustomerFundDetailDTO param) throws Exception {
		CommonUserDTO user = getCommonUser();
		param.setCustId(null);
		param.setAccountName(null);
		param.setBankName(null);
		param.setAccountNumber(null);
		param.setCheckUserId(user.getId());
		param.setCheckUserName(user.getUsername());
		param.setCheckTime(new Date());
	}
	
	private Predicate<CustomerFundDetailDTO> validate() {
		 return s -> ObjectUtil.isEmpty(s.getFundDetailId())
				  || !ObjectUtil.isEmpty(s.getStatus());
		}
}
